package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user/shoppingCart")
@Api(tags = "C端-购物车接口")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    //查询购物车
    @ApiOperation("查询购物车")
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list(){
      List<ShoppingCart> list =  shoppingCartService.cartList();
      return Result.success(list);
    }

    //添加购物车
    @PostMapping("/add")
    @ApiOperation("添加购物车")
    public Result addCart(
            @RequestHeader(value = "Content-Type", required = true) String contentType,
            @RequestBody ShoppingCartDTO shoppingCartDTO) {
        if (!"application/json".equals(contentType)) {
            return Result.error("Content-Type 必须为 application/json");
            // 或者抛自定义异常，统一异常处理返回更规范的错误响应
        }
        shoppingCartService.addCart(shoppingCartDTO);
        return Result.success();
    }

    // 删除购物车
    @ApiOperation("删除购物车")
    @PostMapping("/sub")
    public Result delete(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        shoppingCartService.delete(shoppingCartDTO);
        return Result.success();
    }

    //清空购物车
    @ApiOperation("清空购物车")
    @DeleteMapping("/clean")
    public Result clean() {
        shoppingCartService.clean();
        return Result.success();
    }


}
